﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DataAccess;
using System.Collections;

namespace BussinesLogic
{
    public class Documento : BaseObject
    {
        #region Elementos Privados
        private ArrayList _Folios;
        #endregion
        
        #region Public Properties
        public int id { get; set; }
        public string CodDocumento{ get; set; }
        public string NoExpediente{ get; set; }
        public int NumPaginas{ get; set; }
        public string Descripcion{ get; set; }
        public DateTime FechaDocumento{ get; set; }
        public DateTime FechaRecepcion{ get; set; }
        public string Tipo { get; set; }
        public string Usuario { get; set; }
        public string documento { get; set; }

        public ArrayList Folios
        {
            get 
            {
                if (this._Folios == null || this._Folios.Count == 0)
                {
                    DataAccess.Factory.FolioFactoryDao tmpFactory = new DataAccess.Factory.FolioFactoryDao();
                    this._Folios = tmpFactory.getFoliosByDocument(this.CodDocumento);
                }

                return this._Folios;
            }
            set 
            {
                this._Folios = value;
            }
        }

        #endregion


        /***********************************/

        public Documento()
        {
            this.FechaDocumento = DateTime.Now;
            this.FechaRecepcion = DateTime.Now;

        }

        public string getHML(int pageNo)
        {
            pageNo = pageNo - 1;
            string html = "";
            if (this.Folios != null && pageNo < this.Folios.Count)
            {

                Folio pagina = (Folio)this.Folios[pageNo];

                html += "<div id='pageBody'>";

                foreach (Linea ln in pagina.Lineas)
                {
                    html += "<div class='linea' onmouseover='return onLineOver(this);' onmouseout='return onLineOut(this);'>";

                    html += "<a name='linea" + ln.NumLinea  + "'><span class='noLinea'>" + ln.NumLinea + "</span></a>";

                    html += "<span class='contenidoLinea'>" + ln.Texto + "</span>";

                    html += "</div>";
                }

                html += "</div>";

                html += "<br/><br/>";
            }
            else
            {
                html = "<p>Pagina no existe</p>";
            }
            return html;
        }

        public int getFolioNumForPage(int pageNo)
        {
            DataAccess.Factory.DocumentoFactoryDao tmpFac = new DataAccess.Factory.DocumentoFactoryDao();

            int tmpNum = tmpFac.getNumFolioForNumPage(pageNo, this.CodDocumento);

            

            return tmpNum;
        }

        public static Documento getDocument(string CodDocumento, string NoExpediente)
        {
            DataAccess.Factory.DocumentoFactoryDao tmpFac = new DataAccess.Factory.DocumentoFactoryDao();

            return tmpFac.getDocument(CodDocumento, NoExpediente);
        }
        public static ArrayList getDocuments(string NoExpediente)
        {
            DataAccess.Factory.DocumentoFactoryDao tmpFac = new DataAccess.Factory.DocumentoFactoryDao();

            return tmpFac.getDocuments(NoExpediente);
        }

        public int Create()
        {
            try
            {
                DataAccess.Factory.DocumentoFactoryDao tmpFac = new DataAccess.Factory.DocumentoFactoryDao();
                this.id = tmpFac.createDocumento(this.xmlObjectDescription());
            }
            catch (Exception ex)
            {
                return -1;
            }
            return 0;
        }

        public int Update()
        {
            try
            {
                DataAccess.Factory.DocumentoFactoryDao tmpFac = new DataAccess.Factory.DocumentoFactoryDao();
                this.id = tmpFac.updateDocumento(this.xmlObjectDescription());
            }
            catch (Exception ex)
            {
                return -1;
            }
            return 0;
        }

        public int Adjuntar(string strxml)
        {
            
            try
            {
                DataAccess.Factory.DocumentoFactoryDao tmpFac = new DataAccess.Factory.DocumentoFactoryDao();
                tmpFac.AdjuntaDocumento(strxml);
            }
            catch (Exception ex)
            {
                return -1;
            }
            return 0;
        }

    }
}
